/**   
 * @Title: LotteryInfoAct.java 
 * @Package com.esun.rabbit2.viewer.mainact 
 * @Description: (开奖信息主界面Activity) 
 * @Author Billy qigs@500wan.com   
 * @Date 2011-6-20 上午10:34:08  
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.esun.config.Config;
import com.esun.model.BeidanLatestLotteryInfo;
import com.esun.model.DigitLotteryLatestInfo;
import com.esun.model.LotteryInfoFootballInfo;
import com.esun.network.ConnectionChangeReceiver;
import com.esun.rabbit2.communicaters.lotteryinfo.BasketballLatestMatchInfoCommunicater;
import com.esun.rabbit2.communicaters.lotteryinfo.BeidanLatestLotteryInfoCommunicater;
import com.esun.rabbit2.communicaters.lotteryinfo.DigitLotteryLatestInfoCommunicater;
import com.esun.rabbit2.communicaters.lotteryinfo.FootballLatestMatchInfoCoummunicater;
import com.esun.rabbit2.viewer.mainact.lotteryinfoact.LotteryInfoListAgp;
import com.esun.ui.R;
import com.esun.util.log.EDebug;

public class LotteryInfoAct extends Activity implements OnClickListener {
	/** Called when the activity is first created. */
	private RelativeLayout relativeShuangSeQiu; // 双色球
	private LinearLayout linearShuangseqiu;
	private RelativeLayout relativeDaLeTou; // 大乐透
	private LinearLayout linearDaletou;
	private RelativeLayout relativeShiYiXuanWu; // 11选5
	private LinearLayout linearShiyixuanwu;
	private RelativeLayout relativeFootball;// 精彩足球
	private LinearLayout linearFootball2;
	private RelativeLayout relativeBasketBall; // 竞彩篮球
	private LinearLayout linearBasketBall2;
	private RelativeLayout relativeFucai; // 福彩3D
	private LinearLayout linearFucai;
	private RelativeLayout relativeShishicai; // 时时彩
	private LinearLayout linearShishicai;
	private RelativeLayout relativeShengfu;// 足彩胜负
	private LinearLayout linearShengfu;
	private RelativeLayout relativeRenjiu; // 任选9
	private LinearLayout linearRenjiu;
	private RelativeLayout relativeDanchang; // 北京单场
	private LinearLayout linearDanchang2;
	private RelativeLayout relativeQixing; // 七星彩
	private LinearLayout linearQixing;
	private RelativeLayout relativeQile;// 七乐彩
	private LinearLayout linearQile;
	private RelativeLayout relativeErerxuanwu; // 22选5
	private LinearLayout linearErerxuanwu;
	private RelativeLayout relativePailiesan; // 排列三
	private LinearLayout linearpailiesan;
	private RelativeLayout relativePailiewu;// 排列五
	private LinearLayout linearPailiewu;
	private ImageButton btnRefresh; // 刷新

	private static final int GET_DIGIT_LOTTERYINFO_SUCCEED = 1001; // 通讯成功
	private static final int GET_DIGIT_LOTTERYINFO_FAILED = 1002; // 通讯失败
	private static final int GET_FOOTBALL_LOTTERY_INFO_SUCCEED = 1003;// 获取竞彩足球开奖信息成功
	private static final int GET_FOOTBALL_LOTTERY_INFO_FAILED = 1004;// 获取竞彩足球开奖信息失败
	private static final int GET_BEIDAN_LOTTERY_INFO_SUCCEED = 1005;// 获取北单开奖信息成功
	private static final int GET_BEIDAN_LOTTERY_INFO_FAILED = 1006;// 获取北单开奖信息失败
	private static final int GET_BASKTEBALL_LOTTERY_INFO_SUCCEED = 1007;// 获取竞彩篮球开奖信息成功
	private static final int GET_BASKTEBALL_LOTTERY_INFO_FAILED = 1008;// 获取竞彩篮球开奖信息失败
	private static final int GET_DIGIT_INFO_CONNECTFAILED = 1009;// 获取数字彩开奖信息成功
	private static final int GET_FOOTBALL_INTO_CONNECTFAILED = 1010;// 获取数字彩开奖信息成功
	private static final int GET_BEIDAN_INFO_CONNECTFAILED = 1011;// 北单异常
	private static final int GET_BASKETBALL_INFO_CONNECTFAILED = 1012;// 篮球异常

	private final String STR_GETTING = "获取中...";
	private final String STR_FAILED = "获取失败";
	private final String LOTTERY_ID = "lotid";
	private final String ISFOOTBALL = "isfootball";
	// private final int INDEX_LOTID = 0;
	private final int INDEX_EXPECT = 1;
	private final int INDEX_CODE = 2;
	private final char DOUHAO = ',';
	private final char SPACE = ' ';
	private final String LINE_V = "\\|";
	private final String STR_HEAD = "第";
	private final String STR_TAIL = "期开奖号码";

	private Handler handlerGetCurrentLotteryInfo;
	private ProgressBar pbrRefresh;
	private DigitLotteryLatestInfoCommunicater digitLotteryInfoCommunicater;
	private FootballLatestMatchInfoCoummunicater footballInfoCommunicater;
	private BeidanLatestLotteryInfoCommunicater beidanInfoCommunicater;
	private BasketballLatestMatchInfoCommunicater basketballInfoCommunicater;

	private TextView tvShuangseqiuExpect, tvShuangseqiuRed, tvShuangseqiuBlue;
	private TextView tvShiyixuanwuExpect, tvShiyixuanwuCode;
	private TextView tvFucaisandiExpect, tvFucaisandiCode;
	private TextView tvJingcaizuqiuDate, tvJingcaizuqiuHome, tvJingcaizuqiuScore, tvJingcaizuqiuGuest;
	private TextView tvJingcailanqiuDate, tvJingcailanqiuHome, tvJingcailanqiuScore, tvJingcailanqiuGuest;
	private TextView tvDaletouExpect, tvDaletouRed, tvDaletouBlue;
	private TextView tvShishicaiExpect, tvShishicaiCode;
	private TextView tvShengfucaiExpect, tvShengfucaiCode;
	private TextView tvRenjiuExpect, tvRenjiuCode;
	private TextView tvDanchangExpect, tvDanchangHome, tvDanchangScore, tvDanchangGuest;
	private TextView tvQixingcaiExpect, tvQixingcaiCode;
	private TextView tvQilecaiExpect, tvQilecaiCode;
	private TextView tvErshierxuanwuExpect, tvErshierxuanwuCode;
	private TextView tvPailiesanExpect, tvPailiesanCode;
	private TextView tvPailiewuExpect, tvPailiewuCode;

	private boolean isRunningDigit = false;
	private boolean isRunningFootball = false;
	private boolean isRunningBeidan = false;
	private boolean isRunningBasketball = false;
	private boolean isDigitalEverSucceed = false;// 数字彩是否成功获取过
	private boolean isFootballEverSucceed = false;// 足彩是否成功获取过
	private boolean isBeidanEverSucceed = false;
	private boolean isBasketballEverSucceed = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lotteryinfo_act);

		initUI();// 初始化
		initHandler();
	}

	@Override
	protected void onPause() {
		super.onPause();
		this.unregisterReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver());
	}

	@Override
	protected void onResume() {
		this.registerReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver(), ConnectionChangeReceiver.getIntentFilter());
		super.onResume();
		onRefresh();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		// 跳转到双色球开奖信息列表
		case R.id.lotteryinfoact_relative_ssq:
			Intent intent_ssq = new Intent();
			Bundle bundle_ssq = new Bundle();
			intent_ssq.setClass(LotteryInfoAct.this, LotteryInfoListAgp.class);
			bundle_ssq.putString(LOTTERY_ID, Config.SHUANGSEQIU);
			bundle_ssq.putBoolean("isfootball", false);
			intent_ssq.putExtras(bundle_ssq);
			startActivity(intent_ssq);
			break;

		// 跳转到大乐透开奖信息列表
		case R.id.lotteryinfoact_relative_dlt:
			Intent intent_dlt = new Intent();
			Bundle bundle_dlt = new Bundle();
			intent_dlt.setClass(LotteryInfoAct.this, LotteryInfoListAgp.class);
			bundle_dlt.putString(LOTTERY_ID, Config.DALETOU);
			bundle_dlt.putBoolean("isfootball", false);
			intent_dlt.putExtras(bundle_dlt);
			startActivity(intent_dlt);
			break;

		// 跳转到11选5开奖信息列表
		case R.id.lotteryinfoact_relative_syxw:
			Intent intent_syxw = new Intent();
			Bundle bundle_syxw = new Bundle();
			intent_syxw.setClass(LotteryInfoAct.this, LotteryInfoListAgp.class);
			bundle_syxw.putString(LOTTERY_ID, Config.SHIYIXUANWU);
			bundle_syxw.putBoolean("isfootball", false);
			intent_syxw.putExtras(bundle_syxw);
			startActivity(intent_syxw);
			break;

		// 刷新
		case R.id.lotteryinfoact_btn_refresh:
			pbrRefresh.setVisibility(View.VISIBLE);
			btnRefresh.setImageBitmap(null);
			onRefresh();
			btnRefresh.setEnabled(false);
			break;

		case R.id.lotteryinfoact_relative_football:

			// 竞彩足球
			startActivity(new Intent(LotteryInfoAct.this, LotteryInfoListAgp.class).putExtra(LOTTERY_ID, Config.JINGCAIZUQIU).putExtra("isfootball", true));

			break;

		case R.id.lotteryinfoact_relative_fc:

			// 福彩3D
			startActivity(new Intent(LotteryInfoAct.this, LotteryInfoListAgp.class).putExtra(LOTTERY_ID, Config.FUCAI3D).putExtra("isfootball", false));

			break;

		case R.id.lotteryinfoact_relative_ssc:

			// 时时彩
			startActivity(new Intent(LotteryInfoAct.this, LotteryInfoListAgp.class).putExtra(LOTTERY_ID, Config.FUCAI3D).putExtra(ISFOOTBALL, false));

			break;

		case R.id.lotteryinfoact_relative_sfc:

			// 胜负彩
			startActivity(new Intent(LotteryInfoAct.this, LotteryInfoListAgp.class).putExtra(LOTTERY_ID, Config.SHENGFUCAI).putExtra(ISFOOTBALL, false));

			break;

		case R.id.lotteryinfoact_relative_rj:

			// 任选九
			startActivity(new Intent(LotteryInfoAct.this, LotteryInfoListAgp.class).putExtra(LOTTERY_ID, Config.RENXUANJIU).putExtra(ISFOOTBALL, false));

			break;

		case R.id.lotteryinfoact_relative_qx:

			// 七星彩
			startActivity(new Intent(LotteryInfoAct.this, LotteryInfoListAgp.class).putExtra(LOTTERY_ID, Config.QIXINGCAI).putExtra(ISFOOTBALL, false));

			break;

		case R.id.lotteryinfoact_relative_ql:

			// 七乐彩
			startActivity(new Intent(LotteryInfoAct.this, LotteryInfoListAgp.class).putExtra(LOTTERY_ID, Config.QILECAI).putExtra(ISFOOTBALL, false));

			break;

		case R.id.lotteryinfoact_relative_eexw:

			// 二十二选五
			startActivity(new Intent(LotteryInfoAct.this, LotteryInfoListAgp.class).putExtra(LOTTERY_ID, Config.ERSHIERXUANWU).putExtra(ISFOOTBALL, false));

			break;

		case R.id.lotteryinfoact_relative_pls:

			// 排列三
			startActivity(new Intent(LotteryInfoAct.this, LotteryInfoListAgp.class).putExtra(LOTTERY_ID, Config.PAILIESAN).putExtra(ISFOOTBALL, false));

			break;

		case R.id.lotteryinfoact_relative_plw:

			// 排列五
			startActivity(new Intent(LotteryInfoAct.this, LotteryInfoListAgp.class).putExtra(LOTTERY_ID, Config.PAILIEWU).putExtra(ISFOOTBALL, false));

			break;

		case R.id.lotteryinfoact_relative_bdsf:

			// 北京单场
			startActivity(new Intent(LotteryInfoAct.this, LotteryInfoListAgp.class).putExtra(LOTTERY_ID, Config.BEIJINGZUQIUDANCHANG).putExtra(ISFOOTBALL, true));

			break;

		case R.id.lotteryinfoact_relative_basketball:

			// 竞彩篮球
			startActivity(new Intent(LotteryInfoAct.this, LotteryInfoListAgp.class).putExtra(LOTTERY_ID, Config.JINGCAILANQIU).putExtra(ISFOOTBALL, true));

			break;

		default:
			break;
		}
	}

	// 初始化
	public void initUI() {

		btnRefresh = (ImageButton) findViewById(R.id.lotteryinfoact_btn_refresh);
		btnRefresh.setOnClickListener(this);
		btnRefresh.setEnabled(false);

		pbrRefresh = (ProgressBar) findViewById(R.id.lotteryinfoact_pbr_refresh);
		pbrRefresh.setVisibility(View.INVISIBLE);
		btnRefresh.setImageResource(R.drawable.tbgn_icon_update_20x20);

		relativeShuangSeQiu = (RelativeLayout) findViewById(R.id.lotteryinfoact_relative_ssq);
		relativeShuangSeQiu.setOnClickListener(this);
		linearShuangseqiu = (LinearLayout) findViewById(R.id.lotteryinfoact_linear_ssq);
		tvShuangseqiuExpect = (TextView) findViewById(R.id.lotteryinfoact_tv_ssq_expect);
		tvShuangseqiuRed = (TextView) findViewById(R.id.lotteryinfoact_tv_ssq_red);
		tvShuangseqiuBlue = (TextView) findViewById(R.id.lotteryinfoact_tv_ssq_blue);

		relativeShiYiXuanWu = (RelativeLayout) findViewById(R.id.lotteryinfoact_relative_syxw);
		relativeShiYiXuanWu.setOnClickListener(this);
		linearShiyixuanwu = (LinearLayout) findViewById(R.id.lotteryinfoact_linear_syxw);
		tvShiyixuanwuExpect = (TextView) findViewById(R.id.lotteryinfoact_tv_syxw_expect);
		tvShiyixuanwuCode = (TextView) findViewById(R.id.lotteryinfoact_tv_syxw_code);

		relativeFucai = (RelativeLayout) findViewById(R.id.lotteryinfoact_relative_fc);
		relativeFucai.setOnClickListener(this);
		linearFucai = (LinearLayout) findViewById(R.id.lotteryinfoact_linear_fc);
		tvFucaisandiExpect = (TextView) findViewById(R.id.lotteryinfoact_tv_fc3d_expect);
		tvFucaisandiCode = (TextView) findViewById(R.id.lotteryinfoact_tv_fc3d_code);

		relativeFootball = (RelativeLayout) findViewById(R.id.lotteryinfoact_relative_football);
		relativeFootball.setOnClickListener(this);
		linearFootball2 = (LinearLayout) findViewById(R.id.lotteryinfoact_linear_football_2);
		tvJingcaizuqiuDate = (TextView) findViewById(R.id.lotteryinfoact_tv_football_date);
		tvJingcaizuqiuGuest = (TextView) findViewById(R.id.lotteryinfoact_tv_football_guestTeam);
		tvJingcaizuqiuScore = (TextView) findViewById(R.id.lotteryinfoact_tv_football_score);
		tvJingcaizuqiuHome = (TextView) findViewById(R.id.lotteryinfoact_tv_football_homeTeam);

		relativeBasketBall = (RelativeLayout) findViewById(R.id.lotteryinfoact_relative_basketball);
		relativeBasketBall.setOnClickListener(this);
		linearBasketBall2 = (LinearLayout) findViewById(R.id.lotteryinfoact_linear_basketball_2);
		tvJingcailanqiuDate = (TextView) findViewById(R.id.lotteryinfoact_tv_basketball_date);
		tvJingcailanqiuGuest = (TextView) findViewById(R.id.lotteryinfoact_tv_basketball_guestTeam);
		tvJingcailanqiuHome = (TextView) findViewById(R.id.lotteryinfoact_tv_basketball_homeTeam);
		tvJingcailanqiuScore = (TextView) findViewById(R.id.lotteryinfoact_tv_basketball_score);

		relativeDaLeTou = (RelativeLayout) findViewById(R.id.lotteryinfoact_relative_dlt);
		relativeDaLeTou.setOnClickListener(this);
		linearDaletou = (LinearLayout) findViewById(R.id.lotteryinfoact_linear_dlt);
		tvDaletouExpect = (TextView) findViewById(R.id.lotteryinfoact_tv_dlt_expect);
		tvDaletouRed = (TextView) findViewById(R.id.lotteryinfoact_tv_dlt_red);
		tvDaletouBlue = (TextView) findViewById(R.id.lotteryinfoact_tv_dlt_blue);

		relativeShishicai = (RelativeLayout) findViewById(R.id.lotteryinfoact_relative_ssc);
		relativeShishicai.setOnClickListener(this);
		linearShishicai = (LinearLayout) findViewById(R.id.lotteryinfoact_linear_ssc);
		tvShishicaiExpect = (TextView) findViewById(R.id.lotteryinfoact_tv_ssc_expect);
		tvShishicaiCode = (TextView) findViewById(R.id.lotteryinfoact_tv_ssc_code);

		relativeShengfu = (RelativeLayout) findViewById(R.id.lotteryinfoact_relative_sfc);
		relativeShengfu.setOnClickListener(this);
		linearShengfu = (LinearLayout) findViewById(R.id.lotteryinfoact_linear_sfc);
		tvShengfucaiExpect = (TextView) findViewById(R.id.lotteryinfoact_tv_sfc_expect);
		tvShengfucaiCode = (TextView) findViewById(R.id.lotteryinfoact_tv_sfc_code);

		relativeRenjiu = (RelativeLayout) findViewById(R.id.lotteryinfoact_relative_rj);
		relativeRenjiu.setOnClickListener(this);
		linearRenjiu = (LinearLayout) findViewById(R.id.lotteryinfoact_linear_rj);
		tvRenjiuExpect = (TextView) findViewById(R.id.lotteryinfoact_tv_rxj_expect);
		tvRenjiuCode = (TextView) findViewById(R.id.lotteryinfoact_tv_rxj_code);

		relativeDanchang = (RelativeLayout) findViewById(R.id.lotteryinfoact_relative_bdsf);
		relativeDanchang.setOnClickListener(this);
		linearDanchang2 = (LinearLayout) findViewById(R.id.lotteryinfoact_linear_bdsf_2);
		tvDanchangExpect = (TextView) findViewById(R.id.lotteryinfoact_tv_bdspf_date);
		tvDanchangGuest = (TextView) findViewById(R.id.lotteryinfoact_tv_bdspf_guestTeam);
		tvDanchangHome = (TextView) findViewById(R.id.lotteryinfoact_tv_bdspf_homeTeam);
		tvDanchangScore = (TextView) findViewById(R.id.lotteryinfoact_tv_bdspf_score);

		relativeQixing = (RelativeLayout) findViewById(R.id.lotteryinfoact_relative_qx);
		relativeQixing.setOnClickListener(this);
		linearQixing = (LinearLayout) findViewById(R.id.lotteryinfoact_linear_qx);
		tvQixingcaiExpect = (TextView) findViewById(R.id.lotteryinfoact_tv_qxc_expect);
		tvQixingcaiCode = (TextView) findViewById(R.id.lotteryinfoact_tv_qxc_code);

		relativeQile = (RelativeLayout) findViewById(R.id.lotteryinfoact_relative_ql);
		relativeQile.setOnClickListener(this);
		linearQile = (LinearLayout) findViewById(R.id.lotteryinfoact_linear_ql);
		tvQilecaiExpect = (TextView) findViewById(R.id.lotteryinfoact_tv_qlc_expect);
		tvQilecaiCode = (TextView) findViewById(R.id.lotteryinfoact_tv_qlc_code);

		relativeErerxuanwu = (RelativeLayout) findViewById(R.id.lotteryinfoact_relative_eexw);
		relativeErerxuanwu.setOnClickListener(this);
		linearErerxuanwu = (LinearLayout) findViewById(R.id.lotteryinfoact_linear_eexw);
		tvErshierxuanwuExpect = (TextView) findViewById(R.id.lotteryinfoact_tv_eexw_expect);
		tvErshierxuanwuCode = (TextView) findViewById(R.id.lotteryinfoact_tv_eexw_code);

		relativePailiesan = (RelativeLayout) findViewById(R.id.lotteryinfoact_relative_pls);
		relativePailiesan.setOnClickListener(this);
		linearpailiesan = (LinearLayout) findViewById(R.id.lotteryinfoact_linear_pls);
		tvPailiesanExpect = (TextView) findViewById(R.id.lotteryinfoact_tv_pls_expect);
		tvPailiesanCode = (TextView) findViewById(R.id.lotteryinfoact_tv_pls_code);

		relativePailiewu = (RelativeLayout) findViewById(R.id.lotteryinfoact_relative_plw);
		relativePailiewu.setOnClickListener(this);
		linearPailiewu = (LinearLayout) findViewById(R.id.lotteryinfoact_linear_plw);
		tvPailiewuExpect = (TextView) findViewById(R.id.lotteryinfoact_tv_plw_expect);
		tvPailiewuCode = (TextView) findViewById(R.id.lotteryinfoact_tv_plw_code);

		// 设置初始文字“获取中...”
		gettingTextview_Basketball(STR_GETTING);
		gettingTextview_Beidan(STR_GETTING);
		gettingTextview_Digit(STR_GETTING);
		gettingTextview_Football(STR_GETTING);

		digitLotteryInfoCommunicater = new DigitLotteryLatestInfoCommunicater();
		footballInfoCommunicater = new FootballLatestMatchInfoCoummunicater();
		beidanInfoCommunicater = new BeidanLatestLotteryInfoCommunicater();
		basketballInfoCommunicater = new BasketballLatestMatchInfoCommunicater();

	}

	// 取得最新数字彩开奖信息
	public void getDigitLotteryInfoThread() {
		new Thread() {
			@Override
			public void run() {
				isRunningDigit = true;
				Message msg = new Message();
				try {
					boolean isSucceed = digitLotteryInfoCommunicater.isGetDigitLotteryLatestInfoSucceed();
					if (isSucceed) {
						msg.what = GET_DIGIT_LOTTERYINFO_SUCCEED;
						handlerGetCurrentLotteryInfo.sendMessage(msg);
					} else {
						msg.what = GET_DIGIT_LOTTERYINFO_FAILED;
						handlerGetCurrentLotteryInfo.sendMessage(msg);
					}
				} catch (Exception e) {
					msg.what = GET_DIGIT_INFO_CONNECTFAILED;
					handlerGetCurrentLotteryInfo.sendMessage(msg);
				}
			}

		}.start();
	}

	// 获取最新足球比赛信息
	public void getLatestFootballInfoThread() {
		new Thread() {
			@Override
			public void run() {
				super.run();
				isRunningFootball = true;
				Message msg = new Message();
				try {
					boolean isSucceed = footballInfoCommunicater.isGetFootballLotteryInfo();
					if (isSucceed) {
						msg = handlerGetCurrentLotteryInfo.obtainMessage(GET_FOOTBALL_LOTTERY_INFO_SUCCEED, null);
					} else {
						msg = handlerGetCurrentLotteryInfo.obtainMessage(GET_FOOTBALL_LOTTERY_INFO_FAILED, null);
					}
					handlerGetCurrentLotteryInfo.sendMessage(msg);
				} catch (Exception e) {
					msg = handlerGetCurrentLotteryInfo.obtainMessage(GET_FOOTBALL_INTO_CONNECTFAILED, null);
					handlerGetCurrentLotteryInfo.sendMessage(msg);
				}
			}
		}.start();
	}

	// 获取最新北单比赛信息
	public void getLatestBeidanInfoThread() {
		new Thread() {
			@Override
			public void run() {
				super.run();
				isRunningBeidan = true;
				Message msg = new Message();
				try {
					boolean isSucceed = beidanInfoCommunicater.isGetBeidanLotteryInfo();
					if (isSucceed) {
						msg = handlerGetCurrentLotteryInfo.obtainMessage(GET_BEIDAN_LOTTERY_INFO_SUCCEED, null);
					} else {
						msg = handlerGetCurrentLotteryInfo.obtainMessage(GET_BEIDAN_LOTTERY_INFO_FAILED, null);
					}
					handlerGetCurrentLotteryInfo.sendMessage(msg);
				} catch (Exception e) {
					msg = handlerGetCurrentLotteryInfo.obtainMessage(GET_BEIDAN_INFO_CONNECTFAILED, null);
					handlerGetCurrentLotteryInfo.sendMessage(msg);
				}
			}
		}.start();
	}

	// 获取最新篮球比赛信息
	public void getLatestBasketballInfoThread() {
		new Thread() {
			@Override
			public void run() {
				super.run();
				isRunningBasketball = true;
				Message msg = new Message();
				try {
					boolean isSucceed = basketballInfoCommunicater.isGetBasketballLotteryInfo();
					if (isSucceed) {
						msg = handlerGetCurrentLotteryInfo.obtainMessage(GET_BASKTEBALL_LOTTERY_INFO_SUCCEED, null);
					} else {
						msg = handlerGetCurrentLotteryInfo.obtainMessage(GET_BASKTEBALL_LOTTERY_INFO_FAILED, null);
					}
					handlerGetCurrentLotteryInfo.sendMessage(msg);
				} catch (Exception e) {
					msg = handlerGetCurrentLotteryInfo.obtainMessage(GET_BASKETBALL_INFO_CONNECTFAILED, null);
					handlerGetCurrentLotteryInfo.sendMessage(msg);
				}
			}
		}.start();
	}

	// 消息处理
	public void initHandler() {
		handlerGetCurrentLotteryInfo = new Handler() {
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) {

				case GET_DIGIT_LOTTERYINFO_SUCCEED:

					digitLotteryInfoCommunicater.updateLotteryInfo();
					displayDigitLotteryInfo();
					isRunningDigit = false;
					isDigitalEverSucceed = true;

					break;

				case GET_DIGIT_LOTTERYINFO_FAILED:

					isRunningDigit = false;
					if (!isDigitalEverSucceed) {
						gettingTextview_Digit(STR_FAILED);
					}

					break;

				case GET_DIGIT_INFO_CONNECTFAILED:

					isRunningDigit = false;
					if (!isDigitalEverSucceed) {
						gettingTextview_Digit(STR_FAILED);
					}

					break;

				case GET_FOOTBALL_LOTTERY_INFO_SUCCEED:

					showFootballInfo(footballInfoCommunicater.getLatestFootballInfo());
					isRunningFootball = false;
					isFootballEverSucceed = true;

					break;

				case GET_FOOTBALL_LOTTERY_INFO_FAILED:

					isRunningFootball = false;

					if (!isFootballEverSucceed) {
						gettingTextview_Football(STR_FAILED);
					}

					break;

				case GET_FOOTBALL_INTO_CONNECTFAILED:

					isRunningFootball = false;
					if (!isFootballEverSucceed) {
						gettingTextview_Football(STR_FAILED);
					}

					break;

				case GET_BASKTEBALL_LOTTERY_INFO_SUCCEED:

					showBasketballInfo(basketballInfoCommunicater.getLatestBasketballInfo());
					isRunningBasketball = false;
					isBasketballEverSucceed = true;

					break;

				case GET_BASKTEBALL_LOTTERY_INFO_FAILED:

					isRunningBasketball = false;
					if (!isBasketballEverSucceed) {
						gettingTextview_Basketball(STR_FAILED);
					}

					break;

				case GET_BASKETBALL_INFO_CONNECTFAILED:

					isRunningBasketball = false;
					if (!isBasketballEverSucceed) {
						gettingTextview_Basketball(STR_FAILED);
					}

					break;

				case GET_BEIDAN_LOTTERY_INFO_SUCCEED:

					if (beidanInfoCommunicater.getListResult().size() > 0) {
						showBeidanInfo(beidanInfoCommunicater.getLatestBeidanInfo());
						isRunningBeidan = false;
						isBeidanEverSucceed = true;
					} else {
						if (!isBeidanEverSucceed) {
							gettingTextview_Beidan(STR_FAILED);
						}
					}

					break;

				case GET_BEIDAN_LOTTERY_INFO_FAILED:

					isRunningBeidan = false;
					if (beidanInfoCommunicater.getState() == 224) {
						if (!isBeidanEverSucceed) {
							gettingTextview_Beidan(beidanInfoCommunicater.getErrorMSG());
						}
					} else {
						if (!isBeidanEverSucceed) {
							gettingTextview_Beidan(STR_FAILED);
						}
					}

					break;

				case GET_BEIDAN_INFO_CONNECTFAILED:

					isRunningBeidan = false;
					if (!isBeidanEverSucceed) {
						gettingTextview_Beidan(STR_FAILED);
					}

					break;

				default:
					break;
				}
				// 有一个彩种通信结束后刷新按钮可用
				if (!isRunningDigit || !isRunningFootball || !isRunningBasketball || !isRunningBeidan) {
					pbrRefresh.setVisibility(View.INVISIBLE);
					btnRefresh.setImageResource(R.drawable.tbgn_icon_update_20x20);
					btnRefresh.setEnabled(true);
				}
			}
		};
	}

	 
	/**
	 * 显示数字彩开奖信息
	 */
	private void displayDigitLotteryInfo() {
		//信息类对象
		DigitLotteryLatestInfo latestinfo = DigitLotteryLatestInfo.getDigitLotteryLatestInfo();
		//双色球
		String ssq_expect = latestinfo.getSSQLatestInfo()[INDEX_EXPECT];
		String ssq_code =latestinfo.getSSQLatestInfo()[INDEX_CODE]; 
		showinfo(ssq_expect,ssq_code,linearShuangseqiu,tvShuangseqiuExpect,tvShuangseqiuRed,tvShuangseqiuBlue);

		//十一选五
		String shiyixuanwu_expect = latestinfo.getSYXWLatestInfo()[INDEX_EXPECT];
		String shiyixuanwu_code = latestinfo.getSYXWLatestInfo()[INDEX_CODE];
		showinfo(shiyixuanwu_expect,shiyixuanwu_code,linearShiyixuanwu,tvShiyixuanwuExpect,tvShiyixuanwuCode,null);
		
		//福彩3D
		String fucaisandi_expect = latestinfo.getFC3DLatestInfo()[INDEX_EXPECT];
		String fucaisandi_code = latestinfo.getFC3DLatestInfo()[INDEX_CODE];
		showinfo(fucaisandi_expect,fucaisandi_code,linearFucai,tvFucaisandiExpect,tvFucaisandiCode,null);
		
		//大乐透
		String daletou_expect = latestinfo.getDLTLatestInfo()[INDEX_EXPECT];
		String daletou_code = latestinfo.getDLTLatestInfo()[INDEX_CODE];
		showinfo(daletou_expect,daletou_code,linearDaletou,tvDaletouExpect,tvDaletouRed,tvDaletouBlue);
		
		//时时彩
		String shishicai_expect = latestinfo.getSSCLatestInfo()[INDEX_EXPECT];
		String shishicai_code = latestinfo.getSSCLatestInfo()[INDEX_CODE];
		showinfo(shishicai_expect,shishicai_code,linearShishicai,tvShishicaiExpect,tvShishicaiCode,null);
		
		//胜负彩
		String shengfucai_expect = latestinfo.getRXJLatestInfo()[INDEX_EXPECT];
		String shengfucai_code = latestinfo.getRXJLatestInfo()[INDEX_CODE];
		showinfo(shengfucai_expect,shengfucai_code,linearShengfu,tvShengfucaiExpect,tvShengfucaiCode,null);
		
		//任九
		String renjiu_expect = latestinfo.getRXJLatestInfo()[INDEX_EXPECT];
		String renjiu_code = latestinfo.getRXJLatestInfo()[INDEX_CODE];
		showinfo(renjiu_expect,renjiu_code,linearRenjiu,tvRenjiuExpect,tvRenjiuCode,null);
		
		//七星彩
		String qixingcai_expect = latestinfo.getQXCLatestInfo()[INDEX_EXPECT];
		String qixingcai_code = latestinfo.getQXCLatestInfo()[INDEX_CODE];
		showinfo(qixingcai_expect,qixingcai_code,linearQixing,tvQixingcaiExpect,tvQixingcaiCode,null);
			
		//七乐彩
		String qilecai_expect = latestinfo.getQLCLatestInfo()[INDEX_EXPECT];
		String qilecai_code = latestinfo.getQLCLatestInfo()[INDEX_CODE];
		showinfo(qilecai_expect,qilecai_code,linearQile,tvQilecaiExpect,tvQilecaiCode,null);
			
		//十二选五
		String shierxuanwu_expect = latestinfo.getEEXWLatestInfo()[INDEX_EXPECT];
		String shierxuanwu_code = latestinfo.getEEXWLatestInfo()[INDEX_CODE];
		showinfo(shierxuanwu_expect,shierxuanwu_code,linearErerxuanwu,tvErshierxuanwuExpect,tvErshierxuanwuCode,null);
		
		//排列三
		String pailiesan_expect = latestinfo.getPLSLatestInfo()[INDEX_EXPECT];
		String pailiesan_code = latestinfo.getPLSLatestInfo()[INDEX_CODE];
		showinfo(pailiesan_expect,pailiesan_code,linearpailiesan,tvPailiesanExpect,tvPailiesanCode,null);

		//排列五
		String pailiewu_expect = DigitLotteryLatestInfo.getDigitLotteryLatestInfo().getPLWLatestInfo()[INDEX_EXPECT];
		String pailiewu_code = DigitLotteryLatestInfo.getDigitLotteryLatestInfo().getPLWLatestInfo()[INDEX_CODE];
		showinfo(pailiewu_expect,pailiewu_code,linearPailiewu,tvPailiewuExpect,tvPailiewuCode,null);		

	}

	// 显示足彩信息
	private void showFootballInfo(LotteryInfoFootballInfo info) {

		linearFootball2.setVisibility(View.VISIBLE);
		tvJingcaizuqiuDate.setText("比赛日期:" + info.getMatchDate());
		EDebug.o(this, "football home team = " + info.getHomeTeam());
		tvJingcaizuqiuHome.setText(info.getHomeTeam() + "(" + info.getRangQiu() + ")");
		tvJingcaizuqiuGuest.setText(info.getVisitingTeam());
		tvJingcaizuqiuScore.setText(info.getScroe());

	}

	// 显示篮彩信息
	private void showBasketballInfo(LotteryInfoFootballInfo info) {

		linearBasketBall2.setVisibility(View.VISIBLE);
		tvJingcailanqiuDate.setText("比赛日期:" + info.getMatchDate());
		tvJingcailanqiuHome.setText(info.getHomeTeam());
		tvJingcailanqiuGuest.setText(info.getVisitingTeam());
		tvJingcailanqiuScore.setText(info.getScroe());

	}

	// 显示北京单场信息
	private void showBeidanInfo(BeidanLatestLotteryInfo info) {

		linearDanchang2.setVisibility(View.VISIBLE);
		tvDanchangExpect.setText(STR_HEAD + info.getExpect() + STR_TAIL);
		tvDanchangHome.setText(info.getHomeTeam() + "(" + info.getRangQiu() + ")");
		tvDanchangGuest.setText(info.getGuestTeam());
		tvDanchangScore.setText(info.getScore());

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Activity parent = getParent();
			if (parent != null) {
				parent.onKeyDown(keyCode, event);
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	// 刷新
	private void onRefresh() {

		if (!isRunningDigit) {
			if (!isDigitalEverSucceed) {
				gettingTextview_Digit(STR_GETTING);
			}
			getDigitLotteryInfoThread();
		}
		if (!isRunningFootball) {
			if (!isFootballEverSucceed) {
				gettingTextview_Football(STR_GETTING);
			}
			getLatestFootballInfoThread();
		}
		if (!isRunningBasketball) {
			if (!isBasketballEverSucceed) {
				gettingTextview_Basketball(STR_GETTING);
			}
			getLatestBasketballInfoThread();
		}
		if (!isRunningBeidan) {
			if (!isBeidanEverSucceed) {
				gettingTextview_Beidan(STR_GETTING);
			}
			getLatestBeidanInfoThread();
		}

	}

	// 显示数字彩字符串 获取中、获取失败
	private void gettingTextview_Digit(String str) {

		linearShuangseqiu.setVisibility(View.GONE);
		tvShuangseqiuExpect.setText(str);

		linearShiyixuanwu.setVisibility(View.GONE);
		tvShiyixuanwuExpect.setText(str);

		linearFucai.setVisibility(View.GONE);
		tvFucaisandiExpect.setText(str);

		linearPailiewu.setVisibility(View.GONE);
		tvPailiewuExpect.setText(str);

		linearDaletou.setVisibility(View.GONE);
		tvDaletouExpect.setText(str);

		linearShishicai.setVisibility(View.GONE);
		tvShishicaiExpect.setText(str);

		linearShengfu.setVisibility(View.GONE);
		tvShengfucaiExpect.setText(str);

		linearRenjiu.setVisibility(View.GONE);
		tvRenjiuExpect.setText(str);

		linearpailiesan.setVisibility(View.GONE);
		tvPailiesanExpect.setText(str);

		linearQixing.setVisibility(View.GONE);
		tvQixingcaiExpect.setText(str);

		linearQile.setVisibility(View.GONE);
		tvQilecaiExpect.setText(str);

		linearErerxuanwu.setVisibility(View.GONE);
		tvErshierxuanwuExpect.setText(str);

	}

	// 显示足球文字
	private void gettingTextview_Football(String str) {

		linearFootball2.setVisibility(View.GONE);
		tvJingcaizuqiuDate.setText(str);

	}

	// 显示篮球文字
	private void gettingTextview_Basketball(String str) {

		linearBasketBall2.setVisibility(View.GONE);
		tvJingcailanqiuDate.setText(str);

	}

	// 显示北单文字
	private void gettingTextview_Beidan(String str) {

		linearDanchang2.setVisibility(View.GONE);
		tvDanchangExpect.setText(str);

	}
	
	/**
	 * kael 添加
	 * 判断期号和开奖号码是否为空
	 * @param expect
	 * @param lott_opencode
	 * @return
	 */
	private boolean judgeNull(String expect, String lott_opencode){
		boolean judgenull = true;
		if(expect != null && lott_opencode != null){//期号和开奖号码都有返回
			if(expect.equals(Config.NULLMARK) || lott_opencode.equals(Config.NULLMARK)){//判断期号和开奖号码是否是“”
				judgenull = false;
			}
		}else{//期号和中奖号码都没有返回
				judgenull = false;
			}
		return judgenull;
	}
	
	/**
	 * kael 修改，修复有一个获取失败就全显示获取失败的bug
	 * 控制显示开奖信息
	 * @param expect
	 * @param lott_opencode
	 * @param linear
	 * @param tvExpect
	 * @param opencode_red
	 * @param opencode_blue
	 */
	public void showinfo(String expect , String lott_opencode , LinearLayout linear , TextView tvExpect , TextView opencode_red , TextView opencode_blue){
		//判断期号和开奖号码是否为空
		if(judgeNull(expect,lott_opencode)){//不为空显示linear
			linear.setVisibility(View.VISIBLE);
			tvExpect.setText(STR_HEAD + expect + STR_TAIL);
			//判断是否有红蓝球之分
			if(opencode_blue == null){//没有红蓝球之分
				opencode_red.setText(lott_opencode.replace(DOUHAO, SPACE));
			}else{//有红蓝球之分
				String []code = lott_opencode.split(LINE_V);
				opencode_red.setText(code[0].replace(DOUHAO, SPACE));
				opencode_blue.setText(code[1].replace(DOUHAO, SPACE));
			}
		}else{//为空，linear不显示，显示“获取失败”
			linear.setVisibility(View.GONE);
			tvExpect.setText(STR_FAILED);
		}
	}
}